//
//  CreatWattiingView.m
//  BUS365
//
//  Created by login on 15-10-29.
//  Copyright (c) 2015年 bus365. All rights reserved.
//

#import "CreatWattiingView.h"

@interface CreatWattiingView ()
{
    UIImageView *imageView;
    CGFloat angle;
    UIView * backView;
}
@end

@implementation CreatWattiingView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 140, 100)];
        backView.clipsToBounds = YES;
        backView.layer.cornerRadius = 8 ;
        backView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5];
        backView.centerX = [UIScreen mainScreen].bounds.size.width/2;
        backView.centerY = [UIScreen mainScreen].bounds.size.height/2 - 64;
        [self addSubview:backView];
        
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
        imageView.centerX = backView.width * 0.5;
        imageView.centerY = backView.height * 0.5;
        imageView.image = [UIImage imageNamed:@"busloadding_yuan"];
        [backView addSubview:imageView];
//        [self startAnimation];
        
        UIImageView *center = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35, 20)];
        center.center = imageView.center;
        center.image = [UIImage imageNamed:@"busloadding_bus"];
        [backView addSubview:center];
        
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 1.5;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = 9999;
        
        [imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    return self;
}
-(void)setBackColor:(UIColor *)backColor
{
    _backColor = backColor;
    backView.backgroundColor = backColor;
}
-(void)setBackCenter:(CGPoint)backCenter
{
    _backCenter = backCenter;
    backView.center = backCenter;
}
-(void) startAnimation
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.01];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(endAnimation)];
    imageView.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
    [UIView commitAnimations];
}

-(void)endAnimation
{
    angle += 10;
    [self startAnimation];
}

@end
